import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import api from "./api/api.js"
import qs from 'qs';
import Vant from 'vant';
import 'vant/lib/index.css';
import './assets/css/vant-rest.scss';
import { Toast } from 'vant';
import {i18n, vantLocales} from './lang'
import loading from './components/loading.vue'
import { getPageTitle, passwordEncryption } from './utils/tools';
// import Vconsole from 'vconsole'
// let vConsole = new Vconsole()
// Vue.use(vConsole)

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
vantLocales(i18n.locale)

Vue.component("loading",loading);

Vue.use(Vant);

// Vue.prototype.axios = axios;
Vue.prototype.$wsUrl = api.wsUrl;
Vue.prototype.$basePath = api.baseUrl;
Vue.config.productionTip = false

/* 接收token*/
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    if (window.location.hash.split("?")[1]) {
        var r = window.location.hash.split("?")[1].match(reg); //search,查询？后面的参数，并匹配正则
        if (r != null) return unescape(r[2]);
        return null;
    }
}
// localStorage.setItem("token", getQueryString("token"))
/* 处理token结束 */

Vue.prototype.$futuresList = [];

window.api = api;
// post接口封装
window.$postAxios = function (url, data, fn) {
    if(/market/i.test(url)){

    }else{
        Toast.loading({
            message: '',
            forbidClick: true,
        });
    }
   
    let obj = {
        ...data,
        // token: localStorage.getItem("token") || "",
    };
    obj = passwordEncryption(JSON.stringify(obj));
    // console.log(obj, "请求参数")
	 
    // 接口调用
    axios({
        method: 'post',
        url: url,
        headers:{token:window.localStorage.getItem('token')},
        data: {token: obj},
    }).then(function (response) {
		Toast.clear();
		
        // console.log(response, "response");
        let res = response || {};
        // fn(res);
		
        handleError(fn, res)
    }).catch(function (error) {
       //console.log(error, "error");
	 
	    Toast.clear();
        let obj = {
            code: 500,
            msg: 'System error'
        }
        fn(obj)
    });
};

// 上传文件接口封装
window.$uploadAxios = function (url, data, fn) {
	Toast.loading({
	    message: '',
	    forbidClick: true,
	});

    // 接口调用
    axios({
        method: 'post',
        url: url,
        data: data,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:window.localStorage.getItem('token'),
        },
    }).then(function (response) {
         Toast.clear();
        let res = response || {};
        // fn(res);
        handleError(fn, res)
    }).catch(function (error) {
         Toast.clear();
        let obj = {
            code: 500,
            msg: 'System error'
        }
        fn(obj)
    });
};

// get接口封装
window.$getAxios = function (url, data, fn) {
    // data.token = localStorage.getItem("token") || '';
    data = passwordEncryption(JSON.stringify(data));
    // 接口调用
    axios({
        method: 'get',
        url: url,
        headers:{token:window.localStorage.getItem('token')},
        data: {token: data},
    }).then(function (response) {
        let res = response || {};
        // fn(res);
        handleError(fn, res)
    }).catch(function (error) {
        let obj = {
            code: 500,
            msg: 'System error'
        }
        fn(obj)
    });
};

function handleError(fn, res) {
    if (res.data.code == 100) {
      //  Toast.fail(res.data.msg);
		window.localStorage.removeItem("token");
        setTimeout(() => {
            window.location.href = '#/login'
            // location.reload()
        }, 600)
    } else {
        fn(res.data)
    }
}

router.beforeEach((to, from, next) => {
    // to.meta.title && (document.title = to.meta.title);
    // let token = sessionStorage.getItem("token");
    if (to.meta.title) {
        document.title = getPageTitle(i18n,to.meta.title)
    }
    next();
    // if(token){
    //		next();
    // }else{
    // 	if(to.path == '/login'){
    // 		next();
    // 	} else {
    // 		next('/login');
    // 	}
    // }
})

new Vue({
    i18n,
    router,
    store,
    render: h => h(App)
}).$mount('#app')
